<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form class="verify-form" :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="商品类别" prop="ctgId">
          <el-select v-model="dataForm.ctgId">
            <el-option v-for="item in ctgOptions" :value="item.id" :label="item.name" :key="item.id"></el-option>
          </el-select>
        </el-form-item>
    <el-form-item label="销售类别" prop="type">
        <el-radio-group v-model="dataForm.type">
          <el-radio label="1">商品类型</el-radio>
          <el-radio label="2">销售类型</el-radio>
        </el-radio-group>
    </el-form-item>
    <el-form-item label="名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="名称"></el-input>
    </el-form-item>
    <el-form-item label="编码" prop="code">
      <el-input v-model="dataForm.code" placeholder="编码"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="status">
        <el-radio-group v-model="dataForm.status">
          <el-radio label="0">停用</el-radio>
          <el-radio label="1">启用</el-radio>
        </el-radio-group>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
    import {getEimsCtgSaleObj, addEimsCtgSaleObj, putEimsCtgSaleObj} from '@/api/eims/ctgsale'
    import { getCtgList } from '@/api/eims/ctg'
    export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          ctgId: '',
          type: '1',
          name: '',
          code: '',
          status: '1',
          createTime: '',
          updateTime: '',
          createUser: '',
          modifyUser: '',
          tenantId: '',
          orgId: '',
          unitId: '',
          delFlag: ''
        },
        ctgOptions: [],
        dataRule: {
          ctgId: [
            { required: true, message: '商品类别id不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '销售类别(1-商品类型,2-销售类型)不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '名称不能为空', trigger: 'blur' }
          ],
          code: [
            { required: true, message: '编码不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态(1-启用,0-禁用)不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ],
          createUser: [
            { required: true, message: '创建用户id不能为空', trigger: 'blur' }
          ],
          modifyUser: [
            { required: true, message: '修改用户id不能为空', trigger: 'blur' }
          ],
          tenantId: [
            { required: true, message: '租户id不能为空', trigger: 'blur' }
          ],
          orgId: [
            { required: true, message: '组织id不能为空', trigger: 'blur' }
          ],
          unitId: [
            { required: true, message: '单位id不能为空', trigger: 'blur' }
          ],
          delFlag: [
            { required: true, message: '逻辑删除(1-正常,-1已删除)不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.ctgList()
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getEimsCtgSaleObj(this.dataForm.id).then(response => {
                this.dataForm = response.data.data
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
                putEimsCtgSaleObj(this.dataForm).then(data => {
                    this.$message.success('修改成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                });
            } else {
                addEimsCtgSaleObj(this.dataForm).then(data => {
                    this.$message.success('添加成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                })
            }
          }
        })
      },
      //  获取商品类别
      ctgList() {
        getCtgList().then(response => {
          if(response.data.code == '0'){
            this.ctgOptions = response.data.data
          }else{
            this.$message.error(response.data.msg)
          }
        })
      }
    }
  }
</script>
